package com.lujianfei.gamelearn.mahjong

import MahjongModel
import OnMahjongClickListener
import android.util.Log
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.geometry.Offset
import androidx.lifecycle.ViewModel

class MahjongViewModel :ViewModel() {

    companion object {
        const val TAG = "[MahjongViewModel]"
    }

    // 手牌列表
    val mahjongList = mutableStateListOf<MahjongModel>()

    // 出牌列表
    val mahjongOutList = mutableStateListOf<MahjongModel>()

    var questionList = listOf<QuestionModel>()

    var questionIndex = mutableStateOf(0)

    val questionAnswerResult = mutableStateOf<QuestionAnswerResult?>(null)

    fun init() {
        Log.d(TAG, "init")
        questionList = MahjongQuestionManager.instance.load()

        questionList.firstOrNull().let {question->
            question?.cards?.forEach {
                mahjongList.add(MahjongFactory.instance.clone(type = MahjongType.fromTypeValue(it.type), it.number, mOnMahjongClickListener))
            }
        }
    }

    fun touchOn(offset: Offset) {
        Log.d(TAG, "touchOn")
        if (questionAnswerResult.value != null) {
            return
        }
        mahjongList.forEach {
            if (it.drawInfo.dstRect?.contains(offset.x.toInt(), offset.y.toInt()) == true) {
                it.onClick?.onClick(it)
            }
        }
    }

    fun hasSelected(): Boolean {
        return mahjongList.any { it.isSelected.value }
    }

    fun play() {
        Log.d(TAG, "play")
        mahjongList.forEach {
            if (it.isSelected.value) {
                mahjongOutList.add(it)
            }
        }
        mahjongList.removeAll(mahjongOutList)
        mahjongOutList.firstOrNull()?.let { firstOutCard ->
            val question = questionList[questionIndex.value]
            val answer = "这里应该先打${question.answer.joinToString("或") { it.toName() }}"
            val content = question.commentate
            question.answer.find { MahjongType.fromTypeValue(it.type) ==  firstOutCard.type && firstOutCard.number == it.number}?.let {
                questionAnswerResult.value = QuestionAnswerResult( title = "回答正确", answer = answer, content = content, isCorrect = true)
            }?:run {
                questionAnswerResult.value = QuestionAnswerResult( title = "回答错误", answer = answer, content = content, isCorrect = false)
            }
        }
    }

    fun next() {
        Log.d(TAG, "next")
        ++questionIndex.value
        questionIndex.value %= questionList.size
        questionAnswerResult.value = null
        mahjongOutList.clear()
        mahjongList.clear()
        questionList[questionIndex.value].cards.forEach {
            mahjongList.add(MahjongFactory.instance.clone(type = MahjongType.fromTypeValue(it.type), it.number, mOnMahjongClickListener))
        }
    }

    var lastMahjongModel: MahjongModel? = null
    private val mOnMahjongClickListener = object : OnMahjongClickListener {
        override fun onClick(mahjongModel: MahjongModel) {
            Log.d(TAG, "onClick $mahjongModel")
            lastMahjongModel = mahjongModel
            mahjongModel.isSelected.value = !mahjongModel.isSelected.value
            if (mahjongList.count { it.isSelected.value } > 1) { // 只能选中一个
                mahjongList.filter { it != lastMahjongModel }.forEach { it.isSelected.value = false }
            }
        }
    }
}

data class QuestionAnswerResult(val title:String, val answer:String, val content:String, val isCorrect:Boolean)